home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1996 April / MacFormat CD Edition MF36 (April 1996).iso / Shareware City / Developers / Tools Plus - GUI⁄Event libs / Tools Plus 2.6.1a Evaluat'n Kit / Tools Plus 2.6.1a / User Manual / 20-Memory < prev    next >
INI File  |  1994-09-17  |  18KB  |  319 lines

  1. [Display using Monaco 9]
  2.  
  3.  
  4. 20  Memory
  5. ``````````
  6.  
  7.   This chapter details the routines and processes in Tools Plus that consume memory.  All of Tools Plus and the objects it creates exist in your application’s heap (the memory you allocate to an application in the “Get Info” dialog under MultiFinder or System 7).  The only exception to this is Tools Plus’s global record that is used to keep its internal workings functioning.  The Tools Plus global record is kept on your application’s stack.  Fortunately, the global record is less than 2K, so it consumes relatively little stack space.
  8.  
  9.   Almost all the objects created by Tools Plus routines are relocatable, and are therefore accessed internally by handles.  The use of relocatable objects eliminates memory fragmentation caused by non-relocatable objects (locked handles, or dynamically allocated objects created by using a pointer).  The only non-relocatable objects created by Tools Plus are window records, but this is not a problem providing your application calls InitToolsPlus early in the program.  Calling InitToolsPlus early allocates the window records in memory where they won’t cause any heap fragmentation.
  10.  
  11.   Each routine (Tools Plus or otherwise) consumes stack memory just to be able to execute its own code.  After the routine has completed execution, its stack memory is automatically released.  The user interface elements created by Tools Plus routines (such as buttons, list boxes, etc.) continue to occupy memory in your application’s heap until they are explicitly deleted, or their parent window is closed, or when your application ends.  Tools Plus routines that consume memory only during their execution are not listed.
  12.  
  13.   It is your responsibility to ensure that an operation can be completed with the amount of memory that is available in your application.  For example, testing can determine the exact amount of memory required to open a window with 5 buttons, two list boxes, and 40 editing fields.  Your application should ensure that the memory required to create such a window can be allocated safely before the WindowOpen statement is executed.
  14.  
  15.  
  16.  
  17.  
  18.  
  19. Editing Fields
  20. ``````````````
  21.   Each active editing field (potentially one on each standard window, and one additional field for the tool bar and all floating palettes) creates a temporary workspace to store the field’s edited text.  The workspace expands as the user types characters in the field, up to a limit of 32K per active field.  The workspace is released when an editing field is deactivated.  Note that this workspace is still allocated when a standard window is deactivated.
  22.  
  23.   You should be aware that the user can potentially consume 32K of memory just by continuing to type in an editing field.  This can be averted by using length-limited editing fields, which limit the user to typing as many characters as the field can accommodate.
  24.  
  25.  
  26.  
  27.  
  28.  
  29. Measurements
  30. ````````````
  31.   Measurements of memory consumption are approximate for two very important reasons.  Firstly, many objects contain handles to other objects, and because of this, they consume memory from handle blocks.  Handle blocks are a pool of handles that are available to any process or object that requires them.  When a process or object no longer requires a handle, the handle is released back into the available pool.  Additional memory will not be required providing that the demand for handles never exceeds the available supply.  As soon as the demand for handles exceeds the available supply, an additional block of 64 handles is automatically created, consuming 512 bytes of memory.  An entire block will be created even if only one more handle is needed.
  32.  
  33.   The second reason approximate values are provided instead of exact values, is because the Macintosh’s various managers are not always consistent in the way they allocate memory.  The List Manager, for example, needs over 6K of memory the first time any of its routines are accessed (this is placed in the System heap under MultiFinder or System 7).  Once that 6K kernel is loaded, memory consumption is nominal.  Also, the memory consumed by various processes will likely vary between versions of the System.
  34.  
  35.  
  36.  
  37.  
  38.  
  39. Routines that consume heap space (by category)
  40. ``````````````````````````````````````````````
  41.   The following routines create objects in memory, thereby consuming heap space:
  42.  
  43.   Program Initialization: InitToolsPlus
  44.  
  45.            Event Polling: PollSystem
  46.  
  47.                  Windows: WindowOpen
  48.                           WindowOpenRect
  49.                           WindowTitle
  50.                           ToolBarOpen
  51.  
  52.                  Buttons: NewButton
  53.                           NewButtonRect
  54.                           ButtonTitle
  55.  
  56.          Picture Buttons: NewPictButton
  57.  
  58.              Scroll Bars: NewScrollBar
  59.                           NewScrollBarRect
  60.  
  61.            Editing Field: NewField
  62.                           NewFieldRect
  63.                           ActivateField (if a field is not already
  64.                                          active)
  65.                           ClickInField (if a field is not already
  66.                                         active)
  67.  
  68.               List Boxes: NewListBox
  69.                           NewListBoxRect
  70.                           SetListBoxText
  71.                           InsertListBoxLine
  72.  
  73.                   Menus:  AppleMenu
  74.                           Menu
  75.                           InsertMenuItem
  76.                           RenameItem
  77.  
  78.             Pop-Up Menus: NewPopUp
  79.                           NewPopUpRect
  80.                           PopUpMenu
  81.                           InsertPopUpItem
  82.                           RenamePopUp
  83.  
  84.                  Cursors: CursorShape
  85.                           ResetCursor
  86.                           NewCursorTable
  87.                           CursorZone
  88.                           CursorZoneRect
  89.                           CursorZoneRgn
  90.  
  91.        User Notification: SetNotification
  92.  
  93.         Color Drawing/
  94.         Multiple Screens: NumberOfScreens
  95.                           BeginUpdateScreen
  96.  
  97.  
  98.  
  99.  
  100.  
  101. Routines that consume heap space (alphabetic)
  102. `````````````````````````````````````````````
  103.   The following section alphabetically lists routines that create objects in memory.  You will likely notice that in most cases, the amount of memory consumed is negligible.
  104.  
  105. ActivateField     When an editing field is activated, it uses 8 bytes of
  106.                   memory, plus 1 byte for each character of edited text.
  107.                   When the editing field is deactivated, either by
  108.                   deactivating it, activating another field, clicking in
  109.                   another field, or closing the window, this memory is
  110.                   released.  Note that a user can theoretically type
  111.                   thirty thousand characters (32K) into a field.
  112.  
  113. AppleMenu         The Apple menu uses about 4K bytes of memory.
  114.  
  115. BeginUpdateScreen BeginUpdateScreen allocates about 40 bytes.  This
  116.                   memory is deallocated when EndUpdateScreen is called.
  117.  
  118. ButtonTitle       Each character of the button’s title uses 1 byte of
  119.                   memory.  Memory consumed by any previous title is
  120.                   released.  Deleting the button, or closing its window
  121.                   releases its memory.
  122.  
  123. ClickInField      When an editing field is activated by ClickInField, it
  124.                   uses 8 bytes of memory, plus 1 byte for each character
  125.                   of edited text.  When the editing field is
  126.                   deactivated, either by deactivating it, activating
  127.                   another field, clicking in another field, or closing
  128.                   the window, this memory is released.
  129.  
  130. CursorShape       When a cursor is displayed the first time, the CURS
  131.                   resource is loaded into memory using 16 bytes of
  132.                   memory.  If your application has an animated cursor,
  133.                   the acur resource is also loaded, using 4 bytes plus 4
  134.                   bytes per animation frame.
  135.                      If a resource is flagged as “purgeable” it is
  136.                   automatically unloaded to reclaim memory when needed.
  137.  
  138. CursorZone        Each cursor zone uses about 64 bytes of memory.
  139.                   Additional memory is consumed as the zone’s region
  140.                   becomes more complex.  Deleting a cursor zone releases
  141.                   its memory.
  142.  
  143. CursorZoneRect    (Same as CursorZone)
  144.  
  145. CursorZoneRgn     (Same as CursorZone)
  146.  
  147. DrawIcon          When an icon is displayed the first time, the required
  148.                   icon resource is loaded into memory.  Depending on
  149.                   your icon family, this may consume as much as 2K of
  150.                   memory.
  151.                      If a resource is flagged as “purgeable” it is
  152.                   automatically unloaded to reclaim memory when needed.
  153.  
  154. InitToolsPlus     The amount of memory consumed by InitToolsPlus is
  155.                   depended upon the parameters you supply this routine,
  156.                   and the development environment you are using.  The
  157.                   following formula provides a very good guideline:
  158.                       MoreHandles x 512 bytes (creating additional
  159.                           handle blocks)
  160.                     + 5K in THINK Pascal (automatically allocates 10
  161.                           handle blocks)
  162.                     + MaxWindows x 430 bytes (creating window records)
  163.                     + 70K for Tools Plus libraries (smart linking
  164.                           reduces this amount)
  165.  
  166. InsertListBoxLine Each line in a list box uses about 4 bytes of memory,
  167.                   plus 1 byte per character of text displayed in the
  168.                   line.  Deleting the line in the list box, deleting its
  169.                   containing list box, or closing its parent window
  170.                   releases the memory used by the line.
  171.  
  172. InsertMenuItem    Each menu item uses about 10 bytes of memory plus the
  173.                   length of the item’s text.  Deleting an item releases
  174.                   its memory.
  175.  
  176. InsertPopUpItem   Each pop-up menu item uses about 10 bytes of memory
  177.                   plus the length of the item’s text.  Deleting an item
  178.                   releases its memory.
  179.  
  180. Menu              Each menu uses about 36 bytes of memory.  Each menu
  181.                   item uses 10 bytes plus the length of the item’s
  182.                   title.  Deleting the menu releases its memory and that
  183.                   of its associated items.  Deleting an item releases
  184.                   its memory.
  185.  
  186. NewButton         Each button uses about 106 bytes of memory, plus the
  187.                   length of the button’s title.  Deleting the button, or
  188.                   closing its parent window releases the button’s
  189.                   memory.
  190.  
  191. NewButtonRect     (Same as NewButton)
  192.  
  193. NewCursorTable    Each cursor table uses about 20 bytes of memory.
  194.                   Deleting a cursor table releases its memory and that
  195.                   of its associated zones.
  196.  
  197. NewField          Each editing field uses about 46 bytes of memory,
  198.                   regardless of the number of characters used by the
  199.                   field’s string.  Deleting the editing field, or
  200.                   closing its parent window releases the field’s memory.
  201.  
  202. NewFieldRect      (Same as NewField)
  203.  
  204. NewListBox        Each list box uses about 300 bytes of memory.  The
  205.                   first list box created will consume 6K which is
  206.                   required to load the List Manager (consumes System
  207.                   heap under MultiFinder, or System 7).  Deleting the
  208.                   list box, or closing its parent window releases the
  209.                   list box’s memory and the memory used by its
  210.                   associated lines.
  211.  
  212. NewListBoxRect    (Same as NewListBox)
  213.  
  214. NewPictButton     Each picture button uses about 64 bytes of memory.
  215.                   Deleting the picture button, or closing its parent
  216.                   window releases the button’s memory.  When the
  217.                   button’s image is displayed the first time, the
  218.                   required icon or picture resource is loaded into
  219.                   memory.  Depending on your icon family or picture
  220.                   size, this may consume as much as 2K of memory.
  221.                     If a resource is flagged as “purgeable” it is
  222.                   automatically unloaded to reclaim memory when needed.
  223.  
  224. NewPopUp          Each pop-up menu uses 112 bytes of memory, plus the
  225.                   length of the pop-up menu’s title.  Deleting the
  226.                   pop-up menu, or closing its parent window releases the
  227.                   pop-up menu’s memory and the memory used by its
  228.                   associated items.
  229.  
  230. NewPopUpRect      (Same as NewPopUp)
  231.  
  232. NewScrollBar      Each scroll bar uses about 70 bytes of memory.
  233.                   Deleting the scroll bar, or closing its parent window
  234.                   releases the scroll bar’s memory.
  235.  
  236. NewScrollBarRect  (Sames as NewScrollBar)
  237.  
  238. NumberOfScreens   The amount of memory consumed by NumberOfScreens
  239.                   depends on the number of monitors present when your
  240.                   application runs, and their settings.  In the worst
  241.                   case, about 40 bytes are consumed per monitor.
  242.                      Note that the amount of memory consumed is not
  243.                   incremented each time NumberOfScreens is called.
  244.  
  245. PollSystem        All of Tools Plus’s automatic processes are kept
  246.                   running when your application calls PollSystem.  Some
  247.                   of these processes temporarily allocate memory, while
  248.                   others maintain dynamic records.  It’s a good idea to
  249.                   make sure your application never has less that 8K of
  250.                   contiguous memory at any time.  PollSystem can easily
  251.                   function within those margins.
  252.  
  253. PopUpMenu         Each pop-up menu item uses 10 bytes plus the length of
  254.                   the item’s text.  Deleting the pop-up menu releases
  255.                   its memory and that of its associated items. Deleting
  256.                   an item releases its memory.
  257.  
  258. RenameItem        When a menu item is renamed, it uses 1 byte plus the
  259.                   length of the item’s text.  The memory consumed by the
  260.                   previous name is released.  Deleting the menu releases
  261.                   its memory and that of its associated items. Deleting
  262.                   an item releases its memory.
  263.  
  264. RenamePopUp       When a pop-up menu item is renamed, it uses 1 byte
  265.                   plus the length of the item’s text.  The memory
  266.                   consumed by the previous name is released.  Deleting
  267.                   the pop-up menu releases its memory and that of its
  268.                   associated items.  Deleting an item releases its
  269.                   memory.
  270.  
  271. ResetCursor       When a cursor is displayed the first time, the CURS
  272.                   resource is loaded into memory using 16 bytes of
  273.                   memory.  If a resource is flagged as “purgeable” it is
  274.                   automatically unloaded to reclaim memory when needed.
  275.  
  276. SetListBoxText    Each line in a list box uses about 4 bytes of memory,
  277.                   plus 1 byte per character of text displayed in the
  278.                   line.  Deleting the line in the list box, deleting its
  279.                   containing list box, or closing its parent window
  280.                   releases the memory used by the line.
  281.  
  282. SetNotification   Setting the notification uses about 6 bytes of memory
  283.                   plus the length of the message’s text.  Using
  284.                   SetNotification releases the memory previously used by
  285.                   this routine.
  286.  
  287. ToolBarOpen       The tool bar uses 340 bytes if InitToolsPlus ignores
  288.                   color, and 1,250 bytes if InitToolsPlus uses color.
  289.                   Closing the tool bar releases the memory
  290.  
  291. WindowOpen        Each open window uses 340 bytes if InitToolsPlus
  292.                   ignores color, and 1,250 bytes if InitToolsPlus uses
  293.                   color.  Closing the window releases the memory.
  294.  
  295. WindowOpenRect    (same as WindowOpen)
  296.  
  297. WindowTitle       Each character of the window’s title string uses 1
  298.                   byte of memory.  Memory consumed by any previous title
  299.                   is released.  Closing the window releases title
  300.                   memory.
  301.  
  302.  
  303.  
  304.  
  305.  
  306. The Style Table
  307. ```````````````
  308.   Tools Plus uses a memory-efficient method of storing font information for objects that use styles (buttons, editing fields, list boxes and pop-up menus).  The Style Table keeps a record for each unique combination of font, font size, and font style.  These settings are stored in an 8-byte record that is automatically referenced by all buttons, editing fields, list boxes and pop-up menus that use those settings.
  309.  
  310.   An 8-byte Style Table record is created for each unique combination of font, font size, and font style, regardless of the number of objects (buttons, editing fields, list boxes and pop-up menus) that use the same setting.
  311.  
  312.  
  313.  
  314.  
  315.  
  316. Good memory habits
  317. ``````````````````
  318.   It’s a very good idea to check the amount of available contiguous memory before allocating objects or calling a routine that may need to be loaded from disk (ie: a segment that is not yet in memory).  Do the same when you open a document too, because there are a number of applications out there that bomb just because a user created a document on a 8-meg Mac and tried to open it on another Mac with less memory.  Users perceive this type of behavior as being indicative of an unreliable program (“it keeps bombing on me for no reason”).
  319.